package su.example.sms.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import su.example.sms.domain.dto.StudentPageDTO;
import su.example.sms.domain.dto.StudentPageMapper;
import su.example.sms.domain.entity.Student;
import su.example.sms.service.StudentService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/api/students")
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping
    public ResponseEntity<Student> createStudent(@RequestBody Student student) {
        return ResponseEntity.ok(studentService.createStudent(student));
    }

    @PutMapping
    public ResponseEntity<Student> updateStudent(@RequestBody Student student) {
        return ResponseEntity.ok(studentService.updateStudent(student));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStudent(@PathVariable("id") String id) {
        studentService.deleteStudent(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping
    public ResponseEntity<List<Student>> findAllStudents() {
        return ResponseEntity.ok(studentService.findAllStudents());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Student> findStudentById(@PathVariable("id") String id) {
        return ResponseEntity.ok(studentService.findStudentById(id));
    }

    @PostMapping(value = "/query")
    public ResponseEntity<Page<Student>> queryStudent(@RequestBody StudentPageDTO studentPageDTO) {
        Student student=StudentPageMapper.getStudent(studentPageDTO);
        Pageable pageable= StudentPageMapper.getPageable(studentPageDTO);
        return ResponseEntity.ok(studentService.queryStudent(student,pageable));
    }


}